/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.AlgorithmArrangementBo;
import com.comac.ins.isfd.domain.bo.IsfdAnalysisBo;
import com.comac.ins.isfd.domain.bo.query.IsfdAnalysisQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdAnalysisConfigVo;
import com.comac.ins.isfd.domain.vo.IsfdAnalysisCountObjectVo;
import com.comac.ins.isfd.domain.vo.IsfdAnalysisCountResultObjectVo;
import com.comac.ins.isfd.domain.vo.IsfdAnalysisDisplayNameVo;
import com.comac.ins.isfd.domain.vo.IsfdAnalysisVo;
import com.comac.ins.isfd.service.IIsfdAnalysisService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;

/**
 * 算法controller
 *
 * @author hxloongs
 * @date 2024-7-9
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/analysis")
public class IsfdAnalysisController extends BaseController {
    @Autowired
    private IIsfdAnalysisService iIsfdAnalysisService;

    @SaCheckPermission("isfd:analysis:count")
    @GetMapping("/list/count")
    @Operation(summary = "算法首页数量统计", security = @SecurityRequirement(name = "isfd:analysis:count"))
    public R<List<IsfdAnalysisCountObjectVo>> listCount() {
        return R.ok(iIsfdAnalysisService.listCount());
    }

    @SaCheckPermission("isfd:analysis:count")
    @GetMapping("/list/count/{field}")
    @Operation(summary = "算法列表查询数据统计", security = @SecurityRequirement(name = "isfd:analysis:list"))
    public R<List<IsfdAnalysisCountResultObjectVo>> listCountByListByField(@PathVariable String field) {
        return R.ok(iIsfdAnalysisService.listCountByListByField(field));
    }

    @SaCheckPermission("isfd:analysis:count")
    @GetMapping("/list/time/count")
    @Operation(summary = "算法列表查询时间数据统计", security = @SecurityRequirement(name = "isfd:analysis:list"))
    public R<List<IsfdAnalysisCountResultObjectVo>> listTimeCount(
        @RequestParam(value = "field", required = false) String field,
        @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
        @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        return R.ok(iIsfdAnalysisService.listTimeCount(field, startTime, endTime));
    }


    @SaCheckPermission("isfd:analysis:list")
    @PostMapping("/list")
    @Operation(summary = "算法列表查询接口", security = @SecurityRequirement(name = "isfd:analysis:list"))
    public TableDataInfo<IsfdAnalysisVo> list(@RequestBody IsfdAnalysisQueryBo queryBo) {
        return iIsfdAnalysisService.queryPageList(queryBo);
    }

    @SaCheckPermission("isfd:analysis:add")
    @PostMapping(value = "/add")
    @Operation(summary = "算法添加", security = @SecurityRequirement(name = "isfd:analysis:add"))
    public R<Void> add(@RequestBody IsfdAnalysisBo isfdAnalysisBo) {
        return toAjax(iIsfdAnalysisService.add(isfdAnalysisBo));
    }

    @SaCheckPermission("isfd:analysis:update")
    @PostMapping(value = "/update")
    @Operation(summary = "算法修改", security = @SecurityRequirement(name = "isfd:analysis:update"))
    public R<Void> update(@RequestBody IsfdAnalysisBo isfdAnalysisBo) {
        return toAjax(iIsfdAnalysisService.update(isfdAnalysisBo));
    }

    @SaCheckPermission("isfd:analysis:remove")
    @Log(title = "算法删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{analysisIds}")
    public R<Void> remove(@NotEmpty(message = "算法id不能为空")
                          @PathVariable String[] analysisIds) {
        return toAjax(iIsfdAnalysisService.deleteWithValidByIds(List.of(analysisIds)));
    }


    @SaCheckPermission("isfd:analysis:config:getPageListByOperatorRefId")
    @GetMapping("/config/getPageListByOperatorRefId")
    @Operation(summary = "根据算子id查看当前算子对应的所有算法", security = @SecurityRequirement(name = "isfd:analysis:config:getPageListByOperatorRefId"))
    public TableDataInfo<IsfdAnalysisDisplayNameVo> getPageListByOperatorRefId(@NotEmpty(message = "算子id不能为空") @RequestParam("operatorRefId") String operatorRefId,
                                                                               PageQuery pageQuery) {
        return iIsfdAnalysisService.getPageListByOperatorRefId(operatorRefId, pageQuery);
    }


    @SaCheckPermission("isfd:analysis:config:getAlgorithmArrangement")
    @GetMapping("/config/getAlgorithmArrangement")
    @Operation(summary = "算法编排查询", security = @SecurityRequirement(name = "isfd:analysis:config:getAlgorithmArrangement"))
    public R<List<IsfdAnalysisConfigVo>> getPageListByOperatorRefId(@NotEmpty(message = "算法id不能为空") @RequestParam("analysisRefId") String analysisRefId) {
        return R.ok(iIsfdAnalysisService.getAlgorithmArrangement(analysisRefId));
    }

    @SaCheckPermission("isfd:analysis:config:algorithmArrangementSave")
    @PostMapping("/config/algorithmArrangementSave")
    @Operation(summary = "算法编排 暂存、发布、取消发布", security = @SecurityRequirement(name = "isfd:analysis:config:algorithmArrangementSave"))
    public R<Void> algorithmArrangementSave(@RequestBody AlgorithmArrangementBo algorithmArrangementBo) {
        return toAjax(iIsfdAnalysisService.algorithmArrangementSave(algorithmArrangementBo));
    }
}
